/* eslint-disable no-undef */
$(function () {

	app.init();
});

var app = {
	adminPath:"admin_express",
	init() {
		this.toggleAside();
		this.deleteConfirm();
		this.resizeIframe();
		this.changeStatus();
		this.changeNum();
	},
	deleteConfirm() {
		$('.delete').click(function () {
			var flag = confirm('您确定要删除吗?');
			return flag;
		});
	},
	resizeIframe() {
		var heights = document.documentElement.clientHeight - 100;
		var rightMainObj = document.getElementById('rightMain');
		if(rightMainObj) {
			rightMainObj.height = heights;
		}
	},
	toggleAside() {
		$('.aside h4').click(function () {
			$(this).siblings('ul').slideToggle();
		})
	},
	changeStatus() {//修改状态
		var adminPath=this.adminPath;
		$(".chStatus").click(function () {
			var id = $(this).attr('data-id');
			var model = $(this).attr('data-model');	
			var field = $(this).attr('data-field');
			var el = $(this);
			$.get(`/${adminPath}/changeStatus`, {id,model,field}, function (response) {
				if(response.code == 1) {
					if(el.hasClass('label-success')) {
						el.removeClass('label-success').addClass('label-danger').text('隐藏');
					}else{
						el.removeClass('label-danger').addClass('label-success').text('显示');
					}
				}

			})
		})
	},
	changeNum() {
		var adminPath=this.adminPath;
		$(".chSpanNum").click(function () {
			var id = $(this).attr('data-id');
			var model = $(this).attr('data-model');	
			var field = $(this).attr('data-field');
			var spanEl = $(this);
			var spanNum = $(this).html();
			var input =$("<input value='' />");	
			$(this).html(input);
			$(input).trigger('focus').val(spanNum);
			$(input).click(function(e){
				e.stopPropagation();
			})
			$(input).blur(function () {
				var inputNum = $(this).val();
				if(inputNum >0) {
					spanEl.html(inputNum);
				}else{
					spanEl.html(0);
				}

				spanEl.html(inputNum);
				$.get(`/${adminPath}/changeNum`, {id,model,field,num:inputNum}, function (response) {
					if(response.code == 1) {
						console.log(response);
					}
	
				})
				
			})
		})
	}
};

$(window).resize(function () {
	app.resizeIframe()
})